package com.study.conf;

import com.study.domain.entity.Receiver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by IBM on 2016/5/4.
 */
@Configuration
public class RedisConfiguration {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListener receiverListener){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(receiverListener,new PatternTopic("chat"));
        return container;
    }
    @Bean
    MessageListenerAdapter receiverListener(Receiver receiver){
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(receiver,"receiveMessage");
        return messageListenerAdapter;
    }
    @Bean
    Receiver receiver(CountDownLatch latch){
        return new Receiver(latch);
    }
    @Bean
    CountDownLatch latch(){
        return  new CountDownLatch(1);
    }
    @Bean
    StringRedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }
}
